﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.OrderFreightRecord;
using BCDto.TMS.OrderFreightRecord;
using BCEntity.TMS.OrderFreightRecord;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.TMS.OrderFreightRecord
{
	public class OrderFreightRecordService : IOrderFreightRecordService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IOrderFreightRecordData orderFreightRecordData;
		private readonly string key = string.Format("{0}", EntityNameConst.OrderFreightRecordEntity);

		public OrderFreightRecordService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IOrderFreightRecordData orderFreightRecordData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.orderFreightRecordData = orderFreightRecordData;
		}

		#region 检查订单运费是否存在
		public bool Exists(long orderFreightRecordId)
		{
			return this.orderFreightRecordData.Exists(orderFreightRecordId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加订单运费
		public OrderFreightRecordDto Add(OrderFreightRecordRequestDto requestDto)
		{
			var entity = requestDto.As<OrderFreightRecordEntity>();

			return this.orderFreightRecordData.Add(entity).GetAwaiter().GetResult().As<OrderFreightRecordDto>();
		}
		#endregion

		#region 修改订单运费
		public OrderFreightRecordDto Update(OrderFreightRecordPutDto putDto)
		{
			if (!this.Exists(putDto.OrderFreightRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<OrderFreightRecordEntity>();
			var result = this.orderFreightRecordData.Update(entity).GetAwaiter().GetResult().As<OrderFreightRecordDto>();
			string cacheKey = this.redisService.GetKey(key, entity.OrderFreightRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除订单运费
		public bool Delete(long orderFreightRecordId)
		{
			if (!this.Exists(orderFreightRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.orderFreightRecordData.Delete(orderFreightRecordId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, orderFreightRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取订单运费
		public OrderFreightRecordDto Get(long orderFreightRecordId)
		{
			string cacheKey = this.redisService.GetKey(key, orderFreightRecordId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderFreightRecordData.GetEntity(orderFreightRecordId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<OrderFreightRecordDto>();
		}
		#endregion

		#region 获取订单运费列表
		public IEnumerable<OrderFreightRecordDto> GetList(long orderId)
		{
			string cacheKey = this.redisService.GetKey(key, orderId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderFreightRecordData.GetList(orderId).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderFreightRecordDto>>();
		}
		public IEnumerable<OrderFreightRecordDto> GetList(string orderNo)
		{
			string cacheKey = this.redisService.GetKey(key, orderNo);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderFreightRecordData.GetList(orderNo).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderFreightRecordDto>>();
		}
		#endregion

		#region 获取订单运费分页
		public PageableList<OrderFreightRecordDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderFreightRecordData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<OrderFreightRecordDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<OrderFreightRecordDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}